package com.example.b_308.myapplication.view.user;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.b_308.myapplication.R;
import com.example.b_308.myapplication.controller.UserController;
import com.example.b_308.myapplication.service.IUserView;
import com.example.b_308.myapplication.view.base.BaseActivity;
import com.example.b_308.myapplication.view.main.MainActivity;
import com.example.b_308.myapplication.model.Staff;
import com.example.b_308.myapplication.view.register.RegisterActivity;

/**
 *
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener,IUserView{
    EditText et_phone,et_password;
    ImageView iv_phone_delete,iv_password_delete;
    Button but_login;
    TextView tv_register,tv_foget_password;
    UserController userController;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
//        initDate();
        initListener();
        iv_phone_delete.setVisibility(View.GONE);
        iv_password_delete.setVisibility(View.GONE);
    }

    private void initListener() {
        iv_phone_delete.setOnClickListener(this);
        iv_password_delete.setOnClickListener(this);
        but_login.setOnClickListener(this);
        tv_register.setOnClickListener(this);
        tv_foget_password.setOnClickListener(this);
        et_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.length() == 0){
                    iv_phone_delete.setVisibility(View.GONE);
                }else{
                    iv_phone_delete.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        et_password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.length() == 0){
                    iv_password_delete.setVisibility(View.GONE);
                }else{
                    iv_password_delete.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    private void initDate() {
        userController =new UserController(this);
    }

    private  void initView(){
        getTitleBar().setVisibility(View.GONE);
        setContentView(R.layout.activity_login);

        et_phone=findId(R.id.et_phone);
        et_password=findId(R.id.et_password);
        iv_phone_delete=findId(R.id.iv_phone_delete);
        iv_password_delete=findId(R.id.iv_password_delete);
        but_login=findId(R.id.but_login);
        tv_register=findId(R.id.tv_register);
        tv_foget_password=findId(R.id.tv_foget_password);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_phone_delete:
                et_phone.setText("");
                break;
            case R.id.iv_password_delete:
                et_password.setText("");
                break;
            case R.id.but_login:
//                userController.login(et_phone.getText().toString(),et_password.getText().toString());
                tentTo(MainActivity.class);
                break;
            case R.id.tv_register:
                tentTo(RegisterActivity.class);
                break;
            case R.id.tv_foget_password:
                tentTo(com.example.b_308.myapplication.view.user.ForgetPassActivity.class);
                break;
        }
    }

    @Override
    public void loginSuccess(Staff staff) {

    }

    @Override
    public void registerSuccess() {

    }

    @Override
    public void getCodeSuccess() {

    }
}
